package com.it2297.model;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.it2297.main.App;

public class Trainer extends User {
	private String skills;
	private String qualifications;
	
	public String getSkills() {
		return skills;
	}
	public void setSkills(String skills) {
		this.skills = skills;
	}
	public String getQualifications() {
		return qualifications;
	}
	public void setQualifications(String qualifications) {
		this.qualifications = qualifications;
	}
	public  Trainer getTrainer() {

		Trainer trainer=new Trainer();

		try {
			App.mySqlConnection.open();

			ResultSet resultSet = App.mySqlConnection.executeQuery("SELECT * FROM americano.trainer WHERE role = 'TRAINER'");

				while (resultSet.next()) {
				trainer.setQualifications(resultSet.getString("qualification"));
			    trainer.setSkills(resultSet.getString("skill"));
			}
				
				ResultSet resultSet2 = App.mySqlConnection.executeQuery("SELECT * FROM americano.user WHERE role = 'TRAINER'");

				while (resultSet2.next()) {
					
				trainer.setNric(resultSet2.getString("nric"));
			    trainer.setName(resultSet2.getString("name"));
			    trainer.setEmail(resultSet2.getString("email"));
			    trainer.setAddress(resultSet2.getString("address"));
			    trainer.setCountryOfBirth(resultSet2.getString("birth_country"));
			    trainer.setPhone(resultSet2.getString("phone"));
			    trainer.setGender(resultSet2.getString("gender"));
			    trainer.setEthnic(resultSet2.getString("ethnic"));
			    //trainer.setDob(resultSet2.getDate("dob"));
			    trainer.setStatus(resultSet2.getString("status"));
			    trainer.setDateRegister(resultSet2.getTimestamp("date_register"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}
		
		return trainer;
	}
	private void setAttributes(ResultSet resultSet) {
		try {
			this.setSkills(resultSet.getString("skill"));			
			this.setQualifications(resultSet.getString("qualification"));
			this.setNric(resultSet.getString("nric"));
			this.setName(resultSet.getString("name"));
			this.setEmail(resultSet.getString("email"));
			this.setAddress(resultSet.getString("address"));
			this.setCountryOfBirth(resultSet.getString("birth_country"));
			this.setPhone(resultSet.getString("phone"));
			this.setGender(resultSet.getString("gender"));
			this.setEthnic(resultSet.getString("ethnic"));
			this.setDob(resultSet.getDate("dob"));
			this.setStatus(resultSet.getString("status"));
			this.setDateRegister(resultSet.getTimestamp("date_register"));
			
			
		} catch (SQLException ex) {
			ex.printStackTrace();
		}

	}
	public Trainer getSingle(String nric) {
		try {
			App.mySqlConnection.open();

			ResultSet resultSet = App.mySqlConnection.executeQuery("SELECT * FROM americano.TRAINER_VIEW WHERE nric = " +nric+"");

			

			if (resultSet.next()) {
				this.setAttributes(resultSet);
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}
		
		return this;
	}
	public static  ArrayList<Trainer> getMultiple() {
		ArrayList<Trainer> trainers = new ArrayList<Trainer>();

		try {
			App.mySqlConnection.open();

			
			ResultSet resultSet = App.mySqlConnection.executeQuery("SELECT * FROM americano.TRAINER_VIEW where role='TRAINER' ");

			while (resultSet.next()) {
				Trainer trainer=new Trainer();
				trainer.setAttributes(resultSet);
				trainers.add(trainer);
			}

		} catch (SQLException ex) {
			ex.printStackTrace();
		} finally {
			App.mySqlConnection.close();
		}

		return trainers;
	}
	
	
}
